package com.hejing.Class的理解;

import org.junit.Test;

import java.lang.annotation.ElementType;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * ClassName: ClassTest
 * Package: com.hejing.Class的理解
 * Description:
 *
 * @Author: HeJing
 * @Create: 2023-06-03 19:46
 * @Version: 1.0
 */
public class ClassTest {

    /**
     * 获取Class实例的几种方式:
     */
    @Test
    public void test01() throws ClassNotFoundException {

        // 1、调用运行时类的静态属性: class
        Class clazz1 = User.class;
        System.out.println(clazz1);

        // 2、调用运行时类的对象的getClass()方法
        User u1 = new User();
        Class clazz2 = u1.getClass();
        System.out.println(clazz2);

        System.out.println(clazz1 == clazz2);  // true

        // 3、调用Class类的静态方法forName(String className)
        String className = "com.hejing.Class的理解.User";  // 全类名
        Class<?> clazz3 = Class.forName(className);

        System.out.println(clazz3 == clazz1);  // true

        // 4、使用类的加载器的方式 (了解即可)
        Class clazz4 = ClassLoader.getSystemClassLoader().loadClass("com.hejing.Class的理解.User");
        System.out.println(clazz1 == clazz4);
    }

    /**
     *
     */
    @Test
    public void test02(){
        Class c1 = Object.class;
        Class c2 = Comparable.class;
        Class c3 = String[].class;
        Class c4 = int[][].class;
        Class c5 = ElementType.class;
        Class c6 = int.class;
        Class c7 = Object.class;
        Class c8 = void.class;
        Class c9 = Class.class;

        int[] a = new int[10];
        int[] b = new int[100];
        Class c10 = a.getClass();
        Class c11 = b.getClass();

        // 只要元素类型与维度一样，就是同一个Class
        System.out.println(c10 == c11);  // true


    }


    @Test
    public void test03() {
        String str = "hello world";
        System.out.println(isContainChinese(str));
    }

    public static boolean isContainChinese(String str) {
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(str);
        if (m.find()) {
            return true;
        }
        return false;
    }
}






















